<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use App\Models\OrderSettlement;
use App\Models\OrderSettlementDivid;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderSettlementDividController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '结算单包含的订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrderSettlementDivid);

        $settlement = OrderSettlement::query()->with(['organization', 'organization.type', 'organizationUser'])->findOrFail(request('settlement_id'));

        $grid->header(function ($query) use ($settlement) {
            return view('settlement_info', compact('settlement'));
        });

        $grid->model()->with(['order', 'carbarn', 'station'])->where('settlement_id', $settlement->id);

        $grid->column('id', __('Id'));
        $grid->column('order.sn', '订单号');
        $grid->column('下单时间')->display(function () {
            return $this->order->addTime->format('Y-m-d H:i:s');
        });
        $grid->column('order.payState','支付状态')->display(function () {
            return $this->order->payStateText;
        })->label([
            Order::PAY_STATE_NOT_PAY => 'warning',
            Order::PAY_STATE_PAID => 'success',
        ]);

        $grid->column('order.amount', '金额');
        $grid->column('carbarn.name', '洗车舱');
        $grid->column('station.name', '门店');

        $grid->disableActions();
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->disableFilter();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrderSettlementDivid::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('org_uid', __('Org uid'));
        $show->field('org_id', __('Org id'));
        $show->field('mcstation_id', __('Mcstation id'));
        $show->field('cart_id', __('Cart id'));
        $show->field('order_id', __('Order id'));
        $show->field('divided', __('Divided'));
        $show->field('price', __('Price'));
        $show->field('settlement_id', __('Settlement id'));
        $show->field('created_time', __('Created time'));
        $show->field('updated_time', __('Updated time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OrderSettlementDivid);

        $form->number('org_uid', __('Org uid'));
        $form->number('org_id', __('Org id'));
        $form->number('mcstation_id', __('Mcstation id'));
        $form->number('cart_id', __('Cart id'));
        $form->number('order_id', __('Order id'));
        $form->number('divided', __('Divided'));
        $form->number('price', __('Price'));
        $form->number('settlement_id', __('Settlement id'));

        return $form;
    }
}
